package absolute_zero.com.urbox;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;


/**
 * Service for checking the webserver for pending files
 * @author Charles Francis
 * @version 0.03a
 * 
 */
public class UrBoxService extends Service
{
	/**
	 * Interval between checks in miliseconds
	 */
	public static final int INTERVAL = 500;
	/**
	 * Timer object for scheduling the webserver check
	 */
	private static Timer _timer;
	/**
	 * called when the service is created
	 */
	@Override
	public void onCreate()
	{
		startService();
	}//end onCreate
	/**
	 * called when the service ends
	 */
	@Override
	public void onDestroy()
	{
		stopService();
	}//end onDestroy
	/**
	 * starts the service
	 */
	public void startService()
	{
		if(_timer != null)//there can be only one service to lower the memory footprint
		{
			return;
		}
		else
		{
			_timer = new Timer();
		}
		//schedule the site check
		_timer.scheduleAtFixedRate(new TimerTask()
		{
			public void run()
			{
				//TODO: check server mailbox for information
				//TODO: if there's something there, add notification
				Log.i("UrBox", "Checking webserver for incoming files");
			}//end run
		}, 0, INTERVAL);
		//Log the creation of the service
		Log.i("UrBox", "Service Started");
	}//end startService
	/**
	 * stops the service
	 */
	private void stopService()
	{
		if(_timer != null) //remove all tasks if the timer is created
			_timer.cancel();
		//Log the end of the service
		Log.i("UrBox", "Service Stopped");
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}//end stopService
}//end class UrBoxService